package com.teemor.blog.rpc.fallback;



import com.teemor.blog.bean.condition.TbArticleCondition;
import com.teemor.blog.bean.entity.TbArticle;
import com.teemor.blog.mapper.TbArticleMapper;
import com.teemor.blog.rpc.IArticleSearchRpc;
import com.teemor.common.bean.datasource.input.QryArticlesIPo;
import com.teemor.common.bean.datasource.output.QryArticlesOPo;
import com.teemor.common.bean.model.EsOutputModel;
import com.teemor.common.bean.model.EsQueryModel;
import com.teemor.common.bean.utils.BeanConvertor;
import com.teemor.common.bean.utils.enums.DataStatus;
import feign.hystrix.FallbackFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author zhoulk
 * @date: 2020/3/5
 */
@Component
public class ArticleSearchFallbackFactory implements FallbackFactory<IArticleSearchRpc> {

    @Autowired
    TbArticleMapper articleMapper;

    @Override
    public IArticleSearchRpc create(Throwable cause) {

        return new IArticleSearchRpc() {
            @Override
            public EsOutputModel<QryArticlesOPo> searchArticles(@RequestBody EsQueryModel<QryArticlesIPo> inParam) {
                TbArticleCondition tbArticleCondition = new TbArticleCondition();
                tbArticleCondition.setOrderByClause("create_time desc");
                tbArticleCondition.setDataState(DataStatus.USE.value());
                tbArticleCondition.setAtcState(1);
//                PageHelper.startPage(1, 10);
                List<TbArticle> articles = articleMapper.selectArticleByCondition(tbArticleCondition);
                List<QryArticlesOPo> qryArticlesOPos = BeanConvertor.convertBeanList(articles, QryArticlesOPo.class);
                EsOutputModel<QryArticlesOPo> esOutputModel = new EsOutputModel<>();
                esOutputModel.setData(qryArticlesOPos);
                esOutputModel.setScrollId("-1");
                return esOutputModel;
            }

            @Override
            public Map<String, List<QryArticlesOPo>> test() {
                return null;
            }
        };
    }
}
